package com.android.fragmentdemo.leanback
import android.graphics.Color
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.HeaderItem
import androidx.leanback.widget.ItemBridgeAdapter
import androidx.leanback.widget.ListRow
import androidx.leanback.widget.ListRowPresenter
import androidx.leanback.widget.ListRowView
import androidx.leanback.widget.RowPresenter
import com.android.fragmentdemo.R
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.hengfoxwolf.commonbase.base.databeans.SlipItem
import com.android.fragmentdemo.databinding.ActivityHgvactivityBinding
import java.util.Random
import com.hengfoxwolf.commonbase.base.util.LogX

class HGVUseHActivity : BaseActivity<ActivityHgvactivityBinding>() {

    private val random by lazy {
        Random()
    }

    override fun getLayoutId() = R.layout.activity_hgvactivity

    override fun init(savedInstanceState: Bundle?) {

//        val mAOA = ArrayObjectAdapter(HGVPresenter(this@HGVUseHActivity))
//        mAOA.addAll(0, DataFactoryIp.createDataSet())
//        val mIBA = ItemBridgeAdapter(mAOA)
//        mBinding.hgvList.apply {
//            horizontalSpacing = 10
//            adapter = mIBA
//        }

        val rowAdapter = ArrayObjectAdapter(object : ListRowPresenter() {
            override fun createRowViewHolder(parent: ViewGroup?): RowPresenter.ViewHolder {
                val rowView = ListRowView(this@HGVUseHActivity)
                rowView.gridView.setPadding(0, 0, 0, 0)
                rowView.gridView.horizontalSpacing = 10
                val layoutParams = rowView.gridView.layoutParams as LinearLayout.LayoutParams
                layoutParams.topMargin = 0
                layoutParams.bottomMargin = 0
                layoutParams.leftMargin = 0
                layoutParams.rightMargin = 0
                rowView.gridView.layoutParams = layoutParams
                return ViewHolder(rowView,rowView.gridView,this)
            }

            override fun onBindRowViewHolder(holder: RowPresenter.ViewHolder?, item: Any?) {
                super.onBindRowViewHolder(holder, item)
            }
        })

        for (i in 0.until(10)) {
            val cardAdapter = ArrayObjectAdapter(HGVPresenter(this@HGVUseHActivity))
            val itemCount = random.nextInt(10) + 3
            for (j in 0.until(itemCount)) {
                cardAdapter.add(
                    SlipItem(
                        Color.rgb(
                            random.nextInt(255),
                            random.nextInt(255),
                            random.nextInt(255)
                        ),
                        i * random.nextInt(100)
                    )
                )
            }

            val listRow = ListRow(cardAdapter)
            listRow.headerItem = HeaderItem(i.toLong(), "i--${i + 1}")
            rowAdapter.add(listRow)
        }

        mBinding.vgvList.adapter = ItemBridgeAdapter(rowAdapter)

        //mBinding.vgvList.layoutManager?.getChildAt(0)
        //mBinding.vgvList.layoutManager?.findViewByPosition(0)
    }

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        if (KeyEvent.ACTION_DOWN == event?.action) {
            if (mBinding.btnT0.hasFocus()) {
                when (event.keyCode) {
                    KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_BUTTON_A -> {
                        LogX.d(TAG, "mBinding.btnT0--KeyEvent.KEYCODE_DPAD_CENTER")
                    }

                    else -> {
                        handleKeyEvent(mBinding.btnT0, event)
                    }
                }
            } else if (mBinding.btnT1.hasFocus()) {
                when (event.keyCode) {
                    KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_BUTTON_A -> {
                        LogX.d(TAG, "mBinding.btnT1--KeyEvent.KEYCODE_DPAD_CENTER")
                    }
                    else -> {
                        handleKeyEvent(mBinding.btnT1, event)
                    }
                }
            } else if (mBinding.btnT2.hasFocus()) {
                when (event.keyCode) {
                    KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_BUTTON_A -> {
                        LogX.d(TAG, "mBinding.btnT2--KeyEvent.KEYCODE_DPAD_CENTER")
                    }
                    else -> {
                        handleKeyEvent(mBinding.btnT2, event)
                    }
                }
            } else if (mBinding.vgvList.hasFocus()) {
                when (event.keyCode) {
                    KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_BUTTON_A -> {
                        LogX.d(TAG, "mBinding.vgvList--KeyEvent.KEYCODE_DPAD_CENTER")
                    }
                    else -> {
                        handleKeyEvent(mBinding.vgvList, event)
                    }
                }
            }
        }
        return super.dispatchKeyEvent(event)
    }

    private fun <T : View> handleKeyEvent(focusView: T, event: KeyEvent?) {
        var nextView: View? = null
        when (event?.keyCode) {
            KeyEvent.KEYCODE_DPAD_DOWN -> {
                nextView = focusView.focusSearch(View.FOCUS_DOWN)
            }
            KeyEvent.KEYCODE_DPAD_UP -> {
                nextView = focusView.focusSearch(View.FOCUS_UP)
            }
            KeyEvent.KEYCODE_DPAD_LEFT -> {
                nextView = focusView.focusSearch(View.FOCUS_LEFT)
            }
            KeyEvent.KEYCODE_DPAD_RIGHT -> {
                nextView = focusView.focusSearch(View.FOCUS_RIGHT)
            }
        }
        nextView?.requestFocus()
    }

    companion object {
        const val TAG = "HGVUseHActivity"
    }
}